// app.js

// set up ======================================================================
// get all the tools we 

(function() {
    var childProcess = require("child_process");
    var oldSpawn = childProcess.spawn;
    function mySpawn() {
        // console.log('spawn called');
        // console.log(arguments);
        var result = oldSpawn.apply(this, arguments);
        return result;
    }
    childProcess.spawn = mySpawn;
})();


var express  = require('express');
var subdomain = require('express-subdomain');
var app      = express();
var port     = process.env.PORT || 8081;
var flash    = require('connect-flash');
var path = require('path');
var morgan       = require('morgan');
var cookieParser = require('cookie-parser'); 
var session      = require('express-session');
var configDB = require('./config/database.js');
var mongoose = require('mongoose');
var node_cache = require('node-cache') 
//var connect = require('connect')
var bodyParser   = require('body-parser');
var options = {user: "admin", pass:"lxczo9L(7&"};
var http = require('http');
var server = http.createServer(app);
var md5 = require('md5')
globalCache = new node_cache({stdTTL:  100 * 24 * 3600 ,checkperiod:120});
var redis = require('redis');

//var io = require('socket.io').listen(server);
// configuration ===============================================================

// redisClient = redis.createClient({host : 'localhost', port : 6379});
// redisClient.on('ready',function() {
//  console.log("Redis is ready");
// });

// redisClient.on('error',function() {
//  console.log("Error in Redis");
// });
//var io = require('socket.io').listen(server);
// configuration ===============================================================
//mongoose.connect(configDB.url3,options); // connect to our database
let env = process.env.NODE_WEIBO
console.log(env,'env')
if (env == 'test'){
  mongoose.connect(configDB.url);
}
else{
  mongoose.connect(configDB.url);
}
// console.log((0.1 + 0.2) == 0.3,0.1+0.2, 0.1+0.2==0.3);
// console.log( "i'm a lasagna hog".split("").reverse().join(""))
// config/database.js
// app.all('/*', function(req, res, next) {
//      res.header("Access-Control-Allow-Origin", "*");
//  // // res.header("Access-Control-Allow-Headers", "X-Requested-With");
//      res.header('Access-Control-Allow-Methods', 'GET,POST');
//      res.header('Access-Control-Allow-Headers', 'Content-Type');
//   next();
// });

//require('./config/passport.js')(passport); // pass passport for configuration
// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
//app.use(bodyParser()); // get information from html forms
app.use(express.static(path.join(__dirname, 'public')));

//app.set('view engine', 'ejs'); // set up ejs for templating
// required for passport
app.use(session({ secret: 'iamawesome' , resave : true, saveUninitialized: true,cookie: { maxAge: 0.6 * 24 * 3600000 }})); // session secret
app.use(session({
  genid: function(req) {
    return genuuid() // use UUIDs for session IDs
  },
  secret: 'iamwesome'
}))

app.use(flash()); // use connect-flash for flash messages stored in session
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true,parameterLimit:50000}));
// routes ======================================================================
//var router = require('./routes/routes.js')(app);

//var oo = require('./routes/route_sub.js')(app)
//var router_sub = new express.Router(oo);

// launch ======================================================================
//app.listen(port);
//app.use(subdomain('api', router_sub));
//app.use('/test',router2)
const admin = require('./routes/admin.js')


app.use('/admin',admin)


var wxNotification = require('./util/wxNotification.js')


app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname,'/public/views/app/index2.html'))
});

// app.get('/*', function (req, res) {
//     // res.sendFile(path.join(__dirname,'/public/views/app/index2.html'))
//     res.send({'code':404})
// });


// user related api - myfootprint, useraddress,userlevel, userinfo,mysearch
// product related api - catagory, order , coupon, brand , catagory,topic
// order , transaction related api - transaction , myorder, mycart,cancel, wuliu
// admin . comment, feedback, sale

server.listen(port);
console.log('The magic happens on port ' + port);


